package com.boboPlanet.comm.rabbitmq.producer;

import com.boboPlanet.comm.exception.BusinessException;
import com.boboPlanet.comm.rabbitmq.PlanetCommMQEnum;
import com.boboPlanet.comm.rabbitmq.PlanetMqProducer;
import com.boboPlanet.comm.rabbitmq.factory.MqExchangeFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.*;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2023/6/23
 * @Company planet
 **/
@Slf4j
@Component
public class PlanetMqTopic implements PlanetMqProducer {

    private String queue;

    private String exchange;

    private String routingKey;


    @Resource
    private MqExchangeFactory mqExchangeFactory;

    public PlanetMqTopic queue(String queueName){
        this.queue = queueName;
        return this;
    }

    public PlanetMqTopic exchange(String exchange){
        this.exchange = exchange;
        return this;
    }

    public PlanetMqTopic routingKey(String routingKey){
        this.routingKey = routingKey;
        return this;
    }

    public PlanetMqTopic build(IMqBuild mqEnum){
        this.queue = mqEnum.queue();
        this.exchange = mqEnum.exchange();
        this.routingKey = mqEnum.routingKey();
        return this;
    }

    @Override
    public <T> void sendMsg(IMqBuild build, T data) {
        if (StringUtils.isBlank(build.exchangeType()) || ExchangeTypes.TOPIC.equals(build.exchangeType())){
            mqExchangeFactory.buildTopicMQ(build,data);
        } else if (ExchangeTypes.DIRECT.equals(build.exchangeType())) {
            // direct
            log.info("select direct exchange send MQ");
        } else if (ExchangeTypes.FANOUT.equals(build.exchangeType())) {
            //fanout
            log.info("select fanout exchange send MQ");
        }else {
            // error
            throw BusinessException.build("MQ发送失败,交换机类型不能为空[exchangeType]");
        }
    }

    @Override
    public <T> void sendMsg(T data) {
        PlanetCommMQEnum emptyMq = PlanetCommMQEnum.EMPTY_MQ;
        emptyMq.build(queue,exchange,ExchangeTypes.TOPIC,routingKey,null);

        this.sendMsg(emptyMq,data);

    }

    @Override
    public <T> void sendBatchMsg(IMqBuild build, List<T> data) {

    }


    @Override
    public <T> void sendAsyncMsg(IMqBuild build, T data) {

    }

    @Override
    public <T> void sendAsyncBatchMsg(IMqBuild build, List<T> data) {

    }

}
